package com.wie.controller;

import com.wie.domain.dto.DestroyChinaGdpDTO;
import com.wie.domain.dto.DestroyProvinceGdpDTO;
import com.wie.domain.entity.CountryGdp;
import com.wie.domain.entity.GdpData;
import com.wie.domain.entity.ProvinceGdp;
import com.wie.domain.entity.Result;
import com.wie.service.CountryGdpService;
import com.wie.service.GdpDataService;
import com.wie.service.ProvinceGdpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wie.domain.dto.DestroyCountryGdpDTO;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/destroy")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "true")
public class DestroyDataController {

    @Autowired
    private GdpDataService gdpDataService;
    @Autowired
    private ProvinceGdpService provinceGdpService;
    @Autowired
    private CountryGdpService countryGdpService;

    /**
     * 删除中国GDP数据
     * @param destroyChinaGdpDTO
     * @return
     */
    @DeleteMapping("/china")
    public Result destroyChinaGdpData(@RequestBody DestroyChinaGdpDTO destroyChinaGdpDTO) {
        log.info("删除中国GDP数据：{}", destroyChinaGdpDTO);
        List<GdpData> chinaGdpDataList = gdpDataService.lambdaQuery()
                .ge(destroyChinaGdpDTO.getBeginYear() != null && destroyChinaGdpDTO.getBeginYear() != "", GdpData::getYear, destroyChinaGdpDTO.getBeginYear())
                .le(destroyChinaGdpDTO.getEndYear() != null && destroyChinaGdpDTO.getEndYear() != "", GdpData::getYear, destroyChinaGdpDTO.getEndYear())
                .orderByDesc(GdpData::getYear)
                .list();
        List<Integer> idList = new ArrayList<>();
        for (GdpData gdpData : chinaGdpDataList) {
            Integer id = gdpData.getId();
            idList.add(id);
        }
        gdpDataService.removeBatchByIds(idList);
        return Result.success();
    }

    /**
     * 删除省级GDP数据
     * @param destroyProvinceGdpDTO
     * @return
     */
    @DeleteMapping("/province")
    public Result destroyProvinceGdpData(@RequestBody DestroyProvinceGdpDTO destroyProvinceGdpDTO) {
        log.info("删除省份GDP数据：{}", destroyProvinceGdpDTO);
        List<ProvinceGdp> provinceGdpDataList = provinceGdpService.lambdaQuery()
                .eq(destroyProvinceGdpDTO.getProvince() != null && destroyProvinceGdpDTO.getProvince() != "", ProvinceGdp::getProvince, destroyProvinceGdpDTO.getProvince())
                .ge(destroyProvinceGdpDTO.getBeginYear() != null && destroyProvinceGdpDTO.getBeginYear() != "", ProvinceGdp::getYear, destroyProvinceGdpDTO.getBeginYear())
                .le(destroyProvinceGdpDTO.getEndYear() != null && destroyProvinceGdpDTO.getEndYear() != "", ProvinceGdp::getYear, destroyProvinceGdpDTO.getEndYear())
                .orderByDesc(ProvinceGdp::getYear)
                .list();
        List<Integer> idList = new ArrayList<>();
        for (ProvinceGdp provinceGdp : provinceGdpDataList) {
            Integer id = provinceGdp.getId();
            idList.add(id);
        }
        provinceGdpService.removeBatchByIds(idList);
        return Result.success();
    }

    /**
     * 删除其它国家GDP数据
     * @param destroyCountryGdpDTO
     * @return
     */
    @DeleteMapping("/country")
    public Result destroyCountryGdpData(@RequestBody DestroyCountryGdpDTO destroyCountryGdpDTO) {
        log.info("删除其它国家GDP数据，{}", destroyCountryGdpDTO);
        List<CountryGdp> countyrGdpDataList = countryGdpService.lambdaQuery()
                .eq(destroyCountryGdpDTO.getCountry() != null && destroyCountryGdpDTO.getCountry() != "", CountryGdp::getCountry, destroyCountryGdpDTO.getCountry())
                .ge(destroyCountryGdpDTO.getBeginYear() != null && destroyCountryGdpDTO.getBeginYear() != "", CountryGdp::getYear, destroyCountryGdpDTO.getBeginYear())
                .le(destroyCountryGdpDTO.getEndYear() != null && destroyCountryGdpDTO.getEndYear() != "",CountryGdp::getYear, destroyCountryGdpDTO.getEndYear())
                .orderByDesc(CountryGdp::getYear)
                .list();
        List<Integer> idList = new ArrayList<>();
        for (CountryGdp countryGdp : countyrGdpDataList) {
            Integer id = countryGdp.getId();
            idList.add(id);
        }
        countryGdpService.removeBatchByIds(idList);
        return Result.success();
    }

}
